#include "config.h"
#include "rdb_client.h"

extern int g_num;
extern char* g_prefix;
extern char* g_db;
extern char* g_sock;
extern int g_over;
extern char* g_key;
extern int g_reply;
extern int g_showtime;

int test_rdb_put_array();
int test_rdb_get_array();
int test_rdb_del_array();
int test_rdb_put();
int test_rdb_get();
int test_rdb_del();
int test_rdb_size();

int rdb_trace(const char* obj, const char* msg)
{
	fprintf(stderr, "%s %s\n", obj, msg);
}

int test_rdb_put_array()
{
	rdb* db = 0;
	int i = 0;
	char key[512];
	int res;

	db = rdb_open(g_db, g_sock);
	if (!db) {
		TRACE("! rdb open fail - db=%s\n", g_db);
		return 1;
	}
	rdb_setopt_reply(db, g_reply);
	if (g_showtime) {
        TRACE("rdb open - db=%s\n", g_db);
    }

	for (i=0; i<g_num; i++) {
		int keylen;

		sprintf(key, "%s%d", g_prefix, i);
		keylen = strlen(key) + 1;
		if (g_over)
			res = rdb_replace(db, key, keylen, key, keylen);
		else
			res = rdb_insert(db, key, keylen, key, keylen);
		if (res != 0) {
			TRACE("! rdb insert fail - %s %s\n", key, rdb_strerror(res));
			abort();
		}
	}

	rdb_close(db);
	if (g_showtime) {
        TRACE("rdb close - db=%s\n", g_db);
    }
	return 0;
}

int test_rdb_get_array()
{
	rdb* db = 0;
	int i = 0;
	char key[512];
	int res;

	db = rdb_open(g_db, g_sock);
	if (!db) {
		TRACE("! rdb open fail - db=%s\n", g_db);
		return 1;
	}
	TRACE("rdb open - db=%s\n", g_db);

	for (i=0; i<g_num; i++) {
		char* v;
		int vlen;
		int keylen;

		sprintf(key, "%s%d", g_prefix, i);
		keylen = strlen(key) + 1;
		res = rdb_fetch(db, key, keylen, (void**)&v, &vlen);
		if (res != 0) {
			TRACE("! rdb fetch fail - %s %s\n", key, rdb_strerror(res));
			abort();
		}
	}

	rdb_close(db);
	TRACE("rdb close - db=%s\n", g_db);
	return 0;
}

int test_rdb_put()
{
	rdb* db = 0;
	int i = 0;
	int res;
	char* key = g_key;
	int keylen;

	db = rdb_open(g_db, g_sock);
	if (!db) {
		TRACE("! rdb open fail - db=%s\n", g_db);
		return 1;
	}
	rdb_setopt_reply(db, g_reply);
	TRACE("rdb open - db=%s\n", g_db);

	keylen = strlen(key) + 1;
	if (g_over)
		res = rdb_replace(db, key, keylen, key, keylen);
	else
		res = rdb_insert(db, key, keylen, key, keylen);
	if (res != 0) {
		if (g_over)
			TRACE("! rdb replace fail - %s %s\n", key, rdb_strerror(res));
		else
			TRACE("! rdb insert fail - %s %s\n", key, rdb_strerror(res));
	}
	else {
		if (g_over)
			TRACE("replace ok - v=%s len=%d %s\n", key, keylen, rdb_strerror(res));
		else
			TRACE("insert ok - v=%s len=%d %s\n", key, keylen, rdb_strerror(res));
	}

	rdb_close(db);
	TRACE("rdb close - db=%s\n", g_db);
	return 0;
}

int test_rdb_get()
{
	rdb* db = 0;
	int i = 0;
	int res;
	char* key = g_key;
	int keylen = strlen(g_key) + 1;

	db = rdb_open(g_db, g_sock);
	if (!db) {
		TRACE("! rdb open fail - db=%s\n", g_db);
		return 1;
	}
	TRACE("rdb open - db=%s\n", g_db);

	{
		char* v;
		int vlen;

		res = rdb_fetch(db, key, keylen, (void**)&v, &vlen);
		if (res != 0) {
			TRACE("! rdb fetch fail - %s %s\n", key, rdb_strerror(res));
		}
		else {
			TRACE("fetch ok - v=%s len=%d\n", v, vlen);
		}
	}

	rdb_close(db);
	TRACE("rdb close - db=%s\n", g_db);
	return 0;
}

int test_rdb_del_array()
{
	rdb* db = 0;
	int i = 0;
	char key[512];
	int res;

	db = rdb_open(g_db, g_sock);
	if (!db) {
		TRACE("! rdb open fail - db=%s\n", g_db);
		return 1;
	}
	rdb_setopt_reply(db, g_reply);
	TRACE("rdb open - db=%s\n", g_db);

	for (i=0; i<g_num; i++) {
		int keylen;

		sprintf(key, "%s%d", g_prefix, i);
		keylen = strlen(key) + 1;
		res = rdb_delete(db, key, keylen);
		if (res != 0) {
			TRACE("! rdb del fail - %s %s\n", key, rdb_strerror(res));
		}
	}

	rdb_close(db);
	TRACE("rdb close - db=%s\n", g_db);
	return 0;
}

int test_rdb_del()
{
	rdb* db = 0;
	int i = 0;
	int res;
	char* key = g_key;
	int keylen = strlen(g_key) + 1;

	db = rdb_open(g_db, g_sock);
	if (!db) {
		TRACE("! rdb open fail - db=%s\n", g_db);
		return 1;
	}
	rdb_setopt_reply(db, g_reply);
	TRACE("rdb open - db=%s\n", g_db);

	{
		res = rdb_delete(db, key, keylen);
		if (res != 0) {
			TRACE("! rdb del fail - %s %s\n", key, rdb_strerror(res));
		}
		else {
			TRACE("rdb del ok\n");
		}
	}

	rdb_close(db);
	TRACE("rdb close - db=%s\n", g_db);
	return 0;
}

int test_rdb_size()
{
	rdb* db = 0;
	int i = 0;
	int res;

	db = rdb_open(g_db, g_sock);
	if (!db) {
		TRACE("! rdb open fail - db=%s\n", g_db);
		return 1;
	}
	TRACE("rdb open - db=%s\n", g_db);

	{
		int len = 0;
		res = rdb_size(db, &len);
		if (res != 0) {
			TRACE("! rdb size fail. %s\n", rdb_strerror(res));
		}
		else {
			TRACE("rdb size=%d ok\n", len);
		}
	}

	rdb_close(db);
	TRACE("rdb close - db=%s\n", g_db);
	return 0;
}

